package com.anrongtec.zcpt.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.anrongtec.zcpt.utils.LogUtil;
import com.anrongtec.zcpt.utils.ToolBarHelper;

import java.util.ArrayList;
import java.util.List;

public abstract class IBaseActivity extends AppCompatActivity {
    protected String TAG = "ART_BASE";
    protected Intent intent;
    private ToolBarHelper mToolBarHelper;
    protected Toolbar toolbar;
    private boolean isShowToolBar = true;

    protected void setTitleShow(boolean isShowToolBar) {
        this.isShowToolBar = isShowToolBar;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addActivity(this);
    }

    @Override
    protected void onDestroy() {
        activities.remove(this);
        super.onDestroy();
    }

    @Override
    public void setContentView(int layoutResID) {
        getCurrentActivityName(this);
        if (!isShowToolBar) {
            super.setContentView(layoutResID);
            return;
        }
        mToolBarHelper = new ToolBarHelper(this, layoutResID);
        setContentView(mToolBarHelper.getContentView());
        toolbar = mToolBarHelper.getToolBar();
		/* 自定义的一些操作 */
        onCreateCustomToolBar(toolbar);
		/* 把 toolbar 设置到Activity 中 */
        setSupportActionBar(toolbar);
        onAfterToolBarSettings(toolbar);
    }

    protected void onCreateCustomToolBar(Toolbar toolbar) {
        if (!isShowToolBar) {
            return;
        }
        toolbar.setContentInsetsRelative(0, 0);
    }

    protected void onAfterToolBarSettings(Toolbar toolbar) {
        if (!isShowToolBar) {
            return;
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (!isShowToolBar) {
            return super.onOptionsItemSelected(item);
        }
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /** 初始化视图 **/
    protected void initViews() {

    }

    /** 初始化事件 **/
    protected void initEvents() {
    }

    /**
     * 加载数据
     */
    protected void initData() {

    }

    /**
     * 返回当前activity名称
     *
     * @Title: getRunningActivityName
     * @Description: (这里用一句话描述这个方法的作用)
     * @param @param context
     * @param @return
     * @return String 返回类型
     * @throws
     */
    protected void getCurrentActivityName(Context context) {
        String contextString = context.toString();
        TAG = contextString.substring(contextString.lastIndexOf(".") + 1,
                contextString.indexOf("@"));
    }

    /** 通过Class跳转界面 **/
    protected void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /** 通过Action跳转界面 **/
    protected void startActivity(String action) {
        startActivity(action, null);
    }

    /** 含有Bundle通过Action跳转界面 **/
    protected void startActivity(String action, Bundle bundle) {
        intent = new Intent();
        intent.setAction(action);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /** 含有Bundle通过Class跳转界面 **/
    protected void startActivity(Class<?> cls, Bundle bundle) {
        intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    protected void d(String msg) {
        LogUtil.trace(Log.DEBUG, TAG, msg);
    }

    /**
     *
     * @Title: d
     * @Description: debug输出
     * @param msg
     *            输出消息
     * @param isContinueWrite
     *            :默认是true，即是上面没有这个参数的方法， 表示继续在原有文件写入，如果是false则创建一个新的覆盖原来的它。
     * @throws
     */
    protected void d(String msg, boolean isContinueWrite) {
        LogUtil.trace(Log.DEBUG, TAG, msg, isContinueWrite);
    }

    protected void e(String msg) {
        LogUtil.trace(Log.ERROR, TAG, msg);
    }

    protected void e(String msg, boolean isContinueWrite) {
        LogUtil.trace(Log.ERROR, TAG, msg);
    }

    protected void w(String msg) {
        LogUtil.trace(Log.WARN, TAG, msg);
    }

    protected void w(String msg, boolean isContinueWrite) {
        LogUtil.trace(Log.WARN, TAG, msg);
    }

    protected void i(String msg) {
        LogUtil.trace(Log.INFO, TAG, msg);
    }

    /** 默认退出 **/
    protected void defaultFinish() {
        super.finish();
    }


    public static List<Activity> activities = new ArrayList<Activity>();

    public static void addActivity(Activity activity) {
        activities.add(activity);
    }

    public static void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    public static void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }

}
